package com.masterrabbit.gyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.masterrabbit.common.core.constant.SecurityConstants;
import com.masterrabbit.common.core.context.SecurityContextHolder;
import com.masterrabbit.common.core.utils.JwtUtils;
import com.masterrabbit.common.security.utils.SecurityUtils;
import com.masterrabbit.gyf.Utils.R;
import com.masterrabbit.gyf.domain.Coupons;
import io.jsonwebtoken.Claims;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/mq")
public class TicketProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitAdmin rabbitAdmin;
    //插入优惠券数据到通道
    @PostMapping("/insert")
    public R insert(@RequestBody Coupons coupons) {

        System.out.println("打印商家传来的信息:"+coupons);
//        rabbitTemplate.convertAndSend("myq1",coupons);
        //首先判断现在时间距离投放优惠券的时间还有多长
        LocalDateTime target = coupons.getStartTime().toInstant().atZone(ZoneId.systemDefault())
                .toLocalDateTime();//投放的时间
        LocalDateTime now = LocalDateTime.now();//现在的时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = now.format(formatter);
        //开始计算者两个时间的插值
        long seconds = Duration.between(now, target).getSeconds();
        //生成队列的名字
         String name = UUID.randomUUID().toString();


         //包装参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("coupons", coupons);
        map.put("now", format);

        if(seconds>0){
             Queue queue = QueueBuilder.durable(name).withArgument("x-expires",(int)seconds*1000+10000)
                     .ttl((int)seconds*1000).deadLetterExchange("ex1").build();
             rabbitAdmin.declareQueue(queue);

             rabbitTemplate.convertAndSend(name,JSONObject.toJSONString(map));
         }else{
             System.out.println("目标时间已经过去，无法设置了");
         }

        //开始传入数据
        return R.ok(null);
    }


    //开始消费
    @PostMapping("/consume")
    public String consume() {
        Message o = rabbitTemplate.receive("myq1");
        if(o!=null){
            System.out.println("先打印得到的数据");
            System.out.println(o);
            String string = new String(o.getBody());
            System.out.println(string);
            return string;
        }else{
            return "error";
        }


    }
}
